package Endpoint

import (
	"context"
	"fmt"
	"github.com/go-kit/kit/endpoint"
	Services2 "go-kit/demo4/Services"
)

//定义请求参数结构
type UserRequest struct {
	Uid    int    `json:Uid`
	Method string `json:Method`
}

//定义返回参数结构
type UserResponse struct {
	Result string `json:"result"`
}

//我理解为controller 调用 逻辑层
func GetUserEndPoint(UserService Services2.IGetUserService) endpoint.Endpoint {

	return func(ctx context.Context, request interface{}) (response interface{}, err error) {

		//断言 理解 利用结构 拿到参数 request 就不用理解了 可能是一个 请求对象
		r := request.(UserRequest)

		result := "处理失败"

		fmt.Println(r.Method)

		switch r.Method {
		case "GET":
			//查询
			result = UserService.GetUserName(r.Uid)
			break
		case "POST":
			//写入
			result = UserService.AddUser(r.Uid)
			break
		case "PUT":
			//更新
			result = UserService.UpdateUser(r.Uid)
			break
			//删除
		case "DELETE":
			result = UserService.DelUser(r.Uid)
			break
		}

		//返回数据
		return UserResponse{Result: result}, nil

	}

}
